/*
 * plasticity_array.cpp
 *
 *  Created on: 12.04.2012
 *      Author: delgado
 */

#include "plasticity_array.h"

#include <iostream>
using std::cout;
using std::endl;

namespace neurignacio
{
inline number_t clip(number_t x)
{

	return (x>=1? 1 : 0);
}

Matrix clipped_Hebbian_array(const Pattern_Array& Z, const Topology_Array& W)
{
	Matrix J(W.n, W.n, 0); // Create a nxn Matrix filled with 0
	for (Pattern_Array::const_iterator Zp=Z.begin(); Zp!=Z.end(); ++Zp)
		for (size_t j=0; j<W.height; ++j)
			for (size_t i=0; i<W.width; ++i)
				J(i,j) = J(i,j)+W(i,j)*(*Zp)[i]*(*Zp)[j];
	return J;
}

void test_clipped_array(void)
{
	cout << "Running test_clipped" << endl;
	// Create Empty Topology of size 5
	// meaning network of 5 neurons without connections
	Topology_Array W(5,0);
	// Create connections manually
	W(1,0)=1;
	W(2,1)=1;
	W(3,2)=1;
	W(4,3)=1;
	W(0,4)=1;
	// Send W to stdout
	cout << "Topology W:" << endl;
	cout << W << endl;
	// Create Pattern Z
	Pattern_Array Z(5,0.6);
	// Z0 should be (1,1,1,0,0)
	cout << "Z0: " << endl;
	cout << Z << endl;
	// Generate Patterns
	Z.generate(3);
	// send Z to stdout
	cout << "Pattern Z: " << endl;
	cout << Z << endl;
	// Create Hebbian Matrix
	Matrix J(5,5);
	J = clipped_Hebbian_array(Z,W);
	// send J to stdout
	cout << "Hebbian Matrix: " << endl;
	cout << J << endl;
	cout << "Done!" << endl;

}


void example_clipped_Hebbian_array(void)
{
	cout << "Running example_clipped_Hebbian_array" << endl;
	// Create Empty Topology of size 5
	// meaning network of 5 neurons without connections
	Topology_Array W(5,0);
	// Create connections manually
	W(1,0)=1;
	W(2,1)=1;
	W(3,2)=1;
	W(4,3)=1;
	W(0,4)=1;
	// Send W to stdout
	cout << "Topology W:" << endl;
	cout << W << endl;
	Pattern_Array Z(5,0.6);
	// Z0 should be (1,1,1,0,0)
	cout << "Z0: " << endl;
	cout << Z << endl;
	// Generate Patterns
	// Zp contains an array with Zp(i)=1 if the neuron 'i' is active or Zp(i)=0 otherwise
	vector<uint8> Zp(5,0);
	// Z1
	Zp[0]=1;
	Zp[1]=0;
	Zp[2]=1;
	Zp[3]=1;
	Zp[4]=0;
	Z.push_back(Zp);
	// Z2
	Zp[0]=0;
	Zp[1]=1;
	Zp[2]=0;
	Zp[3]=1;
	Zp[4]=1;
	Z.push_back(Zp);
	// Z3
	Zp[0]=1;
	Zp[1]=1;
	Zp[2]=0;
	Zp[3]=0;
	Zp[4]=0;
	Z.push_back(Zp);
	// send Z to stdout
	cout << "Pattern Z: " << endl;
	cout << Z << endl;
	// Create Hebbian Matrix
	Matrix J(5,5);
	J = clipped_Hebbian_array(Z,W);
	// send J to stdout
	cout << "Hebbian Matrix: " << endl;
	cout << J << endl;
	cout << "Done!" << endl;

}
} // end namespace neurignacio


